package com.pureut.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.system.domain.ProductionWorkOrder;
import com.pureut.system.domain.vo.ProductionWorkOrderVo;
import com.pureut.system.mapper.ProductionWorkOrderMapper;
import com.pureut.system.service.IProductionWorkOrderService;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Random;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * @Author: pureut
 * @Date: 2025-04-09 16:30:36
 * @Desc: 生产工单表
 */
@Service("productionWorkOrderService")
public class ProductionWorkOrderServiceImpl extends ServiceImpl<ProductionWorkOrderMapper, ProductionWorkOrder> implements IProductionWorkOrderService {

    /**
     * 生产工单表Dao
     */
    @Resource
    private ProductionWorkOrderMapper productionWorkOrderMapper;


    /**
     * 新增生产工单表
     * @param productionWorkOrderVo
     * @return
     */
    @Override
    public Boolean save(ProductionWorkOrderVo productionWorkOrderVo) {
        ProductionWorkOrder productionWorkOrder = new ProductionWorkOrder();
        BeanUtil.copyProperties(productionWorkOrderVo, productionWorkOrder);
        productionWorkOrder.setCreator(SecurityUtils.getUsername());
        productionWorkOrder.setCreateTime(new Date());
        productionWorkOrder.setProductionQuantity("0");
        Random random = new Random();
        int randomNumber = 100 + random.nextInt(101); // 100-200
        productionWorkOrder.setStockOnHand(randomNumber+"");
        return save(productionWorkOrder);
    }


    /**
     * 更新生产工单表
     * @param productionWorkOrderVo
     * @return
     */
    @Override
    public Boolean updateById(ProductionWorkOrderVo productionWorkOrderVo) {
        ProductionWorkOrder productionWorkOrder = new ProductionWorkOrder();
        BeanUtil.copyProperties(productionWorkOrderVo, productionWorkOrder);
        productionWorkOrder.setUpdater(SecurityUtils.getUsername());
        productionWorkOrder.setUpdateTime(new Date());
        return updateById(productionWorkOrder);
    }
}
